package com.xyht.sca_s.student_manage_system.modules.patrol.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonCodeEnum;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonResult;
import com.xyht.sca_s.student_manage_system.common.commonReturn.ResponseResult;
import com.xyht.sca_s.student_manage_system.common.util.TimeUtil;
import com.xyht.sca_s.student_manage_system.modules.patrol.entity.SmsPatrol;
import com.xyht.sca_s.student_manage_system.modules.patrol.entity.SmsPatrolPeople;
import com.xyht.sca_s.student_manage_system.modules.patrol.entity.dto.SmsPatrolPeopleDTO;
import com.xyht.sca_s.student_manage_system.modules.patrol.entity.vo.SmsPatrolPeopleVO;
import com.xyht.sca_s.student_manage_system.modules.patrol.mapper.SmsPatrolMapper;
import com.xyht.sca_s.student_manage_system.modules.patrol.mapper.SmsPatrolPeopleMapper;
import com.xyht.sca_s.student_manage_system.modules.patrol.service.SmsPatrolPeopleService;
import com.xyht.sca_s.student_manage_system.modules.user.entity.SmsUser;
import com.xyht.sca_s.student_manage_system.modules.user.mapper.SmsUserMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.xyht.sca_s.student_manage_system.common.util.ParamCheckUtil.isNullOrEmpty;
/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author XYHT
 * @since 2023-04-16
 */
@Service
public class SmsPatrolPeopleServiceImpl extends ServiceImpl<SmsPatrolPeopleMapper,  SmsPatrolPeople> implements SmsPatrolPeopleService {
    @Autowired
    SmsPatrolPeopleMapper smsPatrolPeopleMapper;
    @Autowired
    SmsPatrolMapper smsPatrolMapper;
    @Autowired
    SmsUserMapper smsUserMapper;
    @Override
    public ResponseResult smsPatrolPeopleAdd(SmsPatrolPeopleVO smsPatrolPeopleVO) {
        QueryWrapper<SmsUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SmsUser::getId,smsPatrolPeopleVO.getPatrolUserId());
        SmsUser smsUser = smsUserMapper.selectOne(queryWrapper);
        if(isNullOrEmpty(smsUser)){
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        QueryWrapper<SmsPatrolPeople> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.lambda().eq(SmsPatrolPeople::getPatrolUserId,smsPatrolPeopleVO.getPatrolUserId());
        SmsPatrolPeople smsPatrolPeople = smsPatrolPeopleMapper.selectOne(queryWrapper1);
        if(!isNullOrEmpty(smsPatrolPeople)){
            return CommonResult.failed(CommonCodeEnum.PATROL_USERADD_ERR);
        }
        SmsPatrolPeople smsPatrolPeople1 = new  SmsPatrolPeople();
        smsPatrolPeople1.setPatrolUserId(smsPatrolPeopleVO.getPatrolUserId());
        smsPatrolPeople1.setRealName(smsUser.getRealName());
        smsPatrolPeopleMapper.insert(smsPatrolPeople1);
        return CommonResult.success();
    }

    @Override
    public ResponseResult deleteSmsPatrolPeople(SmsPatrolPeopleVO smsPatrolPeopleVO) {
        if(isNullOrEmpty(smsPatrolPeopleVO.getId())){
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
         SmsPatrolPeople smsPatrolPeople = smsPatrolPeopleMapper.selectById(smsPatrolPeopleVO.getId());
        if(isNullOrEmpty(smsPatrolPeople)){
            return CommonResult.failed(CommonCodeEnum.DATA_NOT_FOUND);
        }
        smsPatrolPeopleMapper.deleteById(smsPatrolPeopleVO.getId());
        return CommonResult.success();
    }

    @Override
    public ResponseResult updateSmsPatrolPeople(SmsPatrolPeopleVO smsPatrolPeopleVO) {
        if(isNullOrEmpty(smsPatrolPeopleVO)){
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
         SmsPatrolPeople query = smsPatrolPeopleMapper.selectById(smsPatrolPeopleVO);
        if(query==null){
            return CommonResult.failed(CommonCodeEnum.DATA_NOT_FOUND);
        }
        SmsPatrolPeople smsPatrolPeople = new  SmsPatrolPeople();
        BeanUtils.copyProperties(smsPatrolPeopleVO,smsPatrolPeople);
        smsPatrolPeopleMapper.updateById(smsPatrolPeople);
        return CommonResult.success();
    }

    @Override
    public ResponseResult selectSmsPatrolPeople(String search_str, Integer pageSize, Integer pageNum, String bg_date, String end_date) {
        Date bgDate = null;
        Date endDate = null;
        if (!isNullOrEmpty(bg_date)) {
            bgDate = TimeUtil.StringtoDate(bg_date);
        }
        if (!isNullOrEmpty(end_date)) {
            endDate = TimeUtil.StringtoDate(end_date);

        }
        QueryWrapper<SmsPatrolPeople> queryWrapper = new QueryWrapper<>();
        if (isNullOrEmpty(queryWrapper)) {
            return CommonResult.failed(CommonCodeEnum.DATA_NOT_FOUND);
        }
        if (bgDate != null) {
            queryWrapper.lambda().ge( SmsPatrolPeople::getCreateTime, bgDate);
        }
        if (endDate != null) {
            queryWrapper.lambda().le( SmsPatrolPeople::getCreateTime, endDate);
        }

        if (!isNullOrEmpty(search_str)) {
            queryWrapper.lambda().like(SmsPatrolPeople::getRealName,search_str);
        }
        List<SmsPatrolPeople> list1 = smsPatrolPeopleMapper.selectList(queryWrapper);
        if(list1.size()==0){
            return CommonResult.failed(CommonCodeEnum.DATA_NOT_FOUND);
        }
        queryWrapper.orderByDesc("create_time");
        Page< SmsPatrolPeople> page = new Page<>(pageNum, pageSize);
        smsPatrolPeopleMapper.selectPage(page, queryWrapper);
        List<SmsPatrolPeople> list = page.getRecords();
        List<SmsPatrolPeopleDTO> list2 = new ArrayList<>();
        if(!isNullOrEmpty(list)&&list.size()>0){
            for(SmsPatrolPeople people : list){
                SmsPatrolPeopleDTO smsPatrolPeopleDTO = new SmsPatrolPeopleDTO();
                QueryWrapper<SmsUser> queryWrapper1 = new QueryWrapper<>();
                queryWrapper1.lambda().eq(SmsUser::getId,people.getPatrolUserId());
                SmsUser smsUser = smsUserMapper.selectOne(queryWrapper1);
                BeanUtils.copyProperties(people,smsPatrolPeopleDTO);
                smsPatrolPeopleDTO.setPatrolUserName(smsUser.getRealName());
                list2.add(smsPatrolPeopleDTO);
            }
       }
        int total = (int) page.getTotal();
        return CommonResult.success(list2, total);
    }

    @Override
    public ResponseResult selectSmsPatrolPeoplePatrolRecord(String search_str, Integer pageSize, Integer pageNum, String bg_date, String end_date,
                                                            String patrol_classes,String name_of_inspection_point) {
        Date bgDate = null;
        Date endDate = null;
        if (!isNullOrEmpty(bg_date)) {
            bgDate = TimeUtil.StringtoDate(bg_date);
        }
        if (!isNullOrEmpty(end_date)) {
            endDate = TimeUtil.StringtoDate(end_date);
        }
        QueryWrapper<SmsPatrol> queryWrapper = new QueryWrapper<>();
        if (isNullOrEmpty(queryWrapper)) {
            return CommonResult.failed(CommonCodeEnum.DATA_NOT_FOUND);
        }
        if (bgDate != null) {
            queryWrapper.lambda().ge( SmsPatrol::getCreateTime, bgDate);
        }
        if (endDate != null) {
            queryWrapper.lambda().le( SmsPatrol::getCreateTime, endDate);
        }

        if (!isNullOrEmpty(search_str)) {
            queryWrapper.lambda().like( SmsPatrol::getPatrolUserId, search_str);
        }
        if(!isNullOrEmpty(patrol_classes)){
            queryWrapper.lambda().like(SmsPatrol::getPatrolClasses,patrol_classes);
        }
        if(!isNullOrEmpty(name_of_inspection_point)){
            queryWrapper.lambda().like(SmsPatrol::getNameOfInspectionPoint,name_of_inspection_point);
        }

        queryWrapper.orderByDesc("create_time");
        Page< SmsPatrol> page = new Page<>(pageNum, pageSize);

        smsPatrolMapper.selectPage(page, queryWrapper);
        List< SmsPatrol> list = page.getRecords();
        int total = (int) page.getTotal();

        return CommonResult.success(list, total);
    }
}
